package com.quentin.servlet;

import java.io.IOException;
import javax.mail.Session;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 * @author Quentin SERVLET
 */
public class Init extends HttpServlet implements Filter {

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HttpSession session = request.getSession(true);

        //redirection vers la page index.html
        if (session.getAttribute("nom") == null || session.getAttribute("nom").toString().isEmpty() || session.getAttribute("nom").toString().compareTo("") == 0) {
            response.sendRedirect("index.jsp");
            //Déconnexion
        } else if (request.getParameter("deconnexion") != null) {
            session.setAttribute("nom", "");
            response.sendRedirect("index.jsp");
        } else {
            session.setAttribute("nom", "");
            response.sendRedirect("index.jsp");/*
             for (int i = 0; i < mGestionnaireMessages.getSize(); i++) {
             response.getWriter().println(mGestionnaireMessages.getMessage(i).getUtilisateur() + " : " + mGestionnaireMessages.getMessage(i).getMessage());
             }*/
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(true);

        if (session.getAttribute("nom") == null || session.getAttribute("nom").toString().isEmpty()) {
            //récupération du login
            String nom = request.getParameter("nom");

            if (nom.isEmpty()) {
                // Erreur, le nom est vide, on redirige vers la page index.html
                response.sendRedirect("index.jsp");
            } else {
                //On sauvegarde le login dans la session
                session.setAttribute("nom", nom);

                //redirection vers la page en frames
                response.sendRedirect("frames.html");
            }
        } else if (request.getParameter("messageAdd") != null) {
            /*mGestionnaireMessages.ajouteMessage(
             new MessageTchat(session.getAttribute("nom").toString(), request.getParameter("messageAdd")));*/
            // response.;
            /*response.addHeader("METHODE", request.getMethod());
             response.setHeader("messageAdd", request.getParameter("messageAdd"));*/
            //response.encodeRedirectURL("messages.jsp");//sendRedirect("messages.jsp");
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "";
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpSession session = req.getSession();
        if (session.getAttribute("nom") == null || session.getAttribute("nom").toString().isEmpty() || session.getAttribute("nom").toString().compareTo("") == 0) {
            ((HttpServletResponse) response).sendRedirect("index.jsp");
        }else{
            chain.doFilter(request, response);
        }
    }
}
